package com.threeox.commonlibrary.util;

import com.threeox.utillibrary.util.EmptyUtils;

import java.util.Map;

/**
 *
 * @ClassName: JSONUtils
 *
 * @Description: TODO(操作JSON的工具类)
 *
 * @author 赵屈犇
 *
 * @date 创建时间: 2017/7/5 18:00
 *
 * @version 1.0
 *
 */
public class JSONUtils {


    /**
     * 根据key的到JSONObject 的数据
     *
     * @param key   可以以.号分割，可以取下tree
     * @return
     */
    public static Object getJSONValue(Map data, String key) {
        if (EmptyUtils.isEmpty(key) || EmptyUtils.isEmpty(data)) {
            return null;
        }
        if (data.containsKey(key)) {
            return data.get(key);
        }
        String[] keys = key.split("\\.");
        Object result = null;
        if (EmptyUtils.isNotEmpty(keys) && keys.length > 1) {
            Map sunObj = data;
            for (int i = 0; i < keys.length - 1; i++) {
                sunObj = getMapByKey(sunObj, keys[i]);
            }
            if (sunObj != null) {
                result = sunObj.get(keys[keys.length - 1]);
            }
            return result;
        } else {
            result = data.get(key);
        }
        return result;
    }

    /**
     * 根据Key值填入数据
     *
     * @param data
     * @param key
     * @param value
     */
    public static void putJSONValue(Map data, String key, Object value) {
        if (EmptyUtils.isEmpty(key) || EmptyUtils.isEmpty(data))
            return;
        if (data.containsKey(key))
            data.put(key, value);
        String[] keys = key.split("\\.");
        if (EmptyUtils.isNotEmpty(keys) && keys.length > 1) {
            Map sunObj = data;
            for (int i = 0; i < keys.length - 1; i++) {
                sunObj = getMapByKey(sunObj, keys[i]);
            }
            if (sunObj != null) {
                sunObj.put(keys[keys.length - 1], value);
            }
        } else {
            data.put(key, value);
        }
    }

    /**
     * 根据Key值删除指定数据
     *
     * @param data
     * @param key
     */
    public static void removeJSONValue(Map data, String key) {
        if (EmptyUtils.isEmpty(key) || EmptyUtils.isEmpty(data))
            return;
        if (data.containsKey(key))
            data.remove(key);
        String[] keys = key.split("\\.");
        if (EmptyUtils.isNotEmpty(keys) && keys.length > 1) {
            Map sunObj = data;
            for (int i = 0; i < keys.length - 1; i++) {
                sunObj = getMapByKey(sunObj, keys[i]);
            }
            if (sunObj != null) {
                sunObj.remove(keys[keys.length - 1]);
            }
        } else {
            data.remove(key);
        }
    }

    /**
     * 根据Key值填入数据,不包含 不存在的Key
     *
     * @param data
     * @param key
     * @param value
     */
    public static void putJSONData(Map data, String key, Object value) {
        if (EmptyUtils.isEmpty(key) || EmptyUtils.isEmpty(data))
            return;
        if (data.containsKey(key))
            data.put(key, value);
        String[] keys = key.split("\\.");
        if (EmptyUtils.isNotEmpty(keys) && keys.length > 1) {
            Map sunObj = data;
            for (int i = 0; i < keys.length - 1; i++) {
                sunObj = getMapByKey(sunObj, keys[i]);
            }
            if (sunObj != null) {
                sunObj.put(keys[keys.length - 1], value);
            }
        }
    }


    /**
     * 得到Map根据Key值
     *
     * @param map
     * @param key
     * @return
     */
    private static Map getMapByKey(Map map, String key) {
        if (map != null) {
            Object res = map.get(key);
            if (res != null && res instanceof Map) {
                return (Map) res;
            }
        }
        return map;
    }

}
